--#include "data\config\activityconfig\DailyActivity\WorshipMonarchConfig.lua" once   --膜拜君主

WorshipMonarchRankColumn = 
{	
	{"curFloor"},				--0，玩家当前膜拜楼层
}

WorshipMonarchRank = Lang.Rank.WorshipMonarchRank		--


function GmDoWorship( sysarg, flag, param )
	if flag == 1 then
		if param == 1 then
			WorshipMonarchStart(  )
		else
			WorshipMonarchStop(  )
		end
	elseif flag == 2 then
		DoWorshipMonarchDoWorship( sysarg, param )
	end
end


function InitTwelvePalacesRank()
	--print("--->InitTwelvePalacesRank...")
	Rank.Init(WorshipMonarchRank, WorshipMonarchRank, WorshipMonarchRankColumn, 1000, 0)
end

function WorshipMonarchStart(  )
	ClearCommonRank( WorshipMonarchRank )		--清除排行榜
	local dVar = System.getDyanmicVar()
	dVar.worshipMonarchTime = System.getCurrMiniTime()

	System.broadcastTipmsgLimitLev(Lang.ScriptTips.WorshipMonarch04, ttScreenCenter+ttChatWindow, 50)
end


function WorshipMonarchStop(  )
	local dVar = System.getDyanmicVar()
	dVar.worshipMonarchTime = nil
	System.broadcastTipmsgLimitLev(Lang.ScriptTips.WorshipMonarch05, ttScreenCenter+ttChatWindow, 50)
end

function WorshipMonarchDoWorship( sysarg, packet )
	local worshipType = DataPack.readByte(packet)
	DoWorshipMonarchDoWorship( sysarg, worshipType )
end

function DoWorshipMonarchDoWorship( sysarg, worshipType )
	--print("WorshipMonarchDoWorship, worshipType="..worshipType)
	if not IsInWorshipMonarch() then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.WorshipMonarch01, ttFlyTip)
		return
	end

	local actorId  = Actor.getActorId( sysarg )
	local curFloor = WorshipMonarchGetCurFloor( actorId )
	--print("WorshipMonarchDoWorship, curFloor="..curFloor)
	if curFloor >= #WorshipMonarchCfg then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.WorshipMonarch02, ttFlyTip)
		return
	end

	local nextWorshCfg = WorshipMonarchCfg[curFloor+1]
	if not nextWorshCfg then
		return
	end

	if nextWorshCfg.needLevel and nextWorshCfg.needLevel > 0 and not Actor.checkActorLevel(sysarg, nextWorshCfg.needLevel, 0) then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.COM0055, nextWorshCfg.needLevel),ttFlyTip)
		return
	end

	if nextWorshCfg.needVip and nextWorshCfg.needVip > 0 and Actor.getVipGrade( sysarg ) < nextWorshCfg.needVip then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.COM0050, nextWorshCfg.needVip),ttFlyTip)
		return
	end

	local consume 	= nextWorshCfg.worship[worshipType].consume
	local rate		= nextWorshCfg.worship[worshipType].rate
	if not consume then
		return
	end
	--判断费用是否足够
	if not CheckConsumeCond( sysarg, consume, 1 ) then
		return
	end

	--扣除费用
	if not DoConsumeCond( sysarg, consume, 1, GameLog.clWorshipMonarchFee, Lang.ScriptTips.WorshipMonarchLog01 or "") then
		return
	end

	local randNum = math.random(1, 100)
	if randNum > rate then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.WorshipMonarch03, ttFlyTip)
		return
	end

	WorshipMonarchClearBuf( sysarg )

	local newFloor = curFloor + 1
	WorshipMonarchSetCurFloor( actorId, newFloor )
	WorshipMonarchSendData( sysarg )

	WorshipMonarchAddBuf( sysarg, nextWorshCfg )
	Actor.collectOperate(sysarg, 0)						--刷属性

	if curFloor == 1 then
		AddActorDailyDoneTimes( sysarg, enDailyOpTimes_WorshipMonarch, nil, 1 )
	end
	
	Actor.triggerAchieveEvent(sysarg, aAchieveEventFinishActivity, 3, newFloor)
end

function IsInWorshipMonarch()
	local dVar = System.getDyanmicVar()
	if not dVar.worshipMonarchTime then
		return false
	end
	return dVar.worshipMonarchTime > 0
end

function WorshipMonarchGetFloor( sysarg )
	return 0
end

function WorshipMonarchGetCurFloor( actorId )
	return GetCommonRankColumnValueById(actorId, WorshipMonarchRank, 0) or 0
end

function WorshipMonarchSetCurFloor( actorId, newFloor )
	return SetCommonRankColumnValueById(actorId, WorshipMonarchRank, 0, 0, newFloor)
end

function WorshipMonarchClearBuf( sysarg )
	local job = Actor.getIntProperty(sysarg,PROP_ACTOR_VOCATION)
	for idx, worshCfg in ipairs( WorshipMonarchCfg ) do
		local jobBufs = worshCfg.buff[job] or {}
		for i, buffId in ipairs( jobBufs ) do
			if Actor.hasBuffById( sysarg, buffId ) then
				Actor.delBuffById( sysarg, buffId )
			end
		end
	end
end

function WorshipMonarchAddBuf( sysarg, worshCfg )
	local job = Actor.getIntProperty(sysarg,PROP_ACTOR_VOCATION)
	--print("WorshipMonarchAddBuf, job="..job)
	local jobBufs = worshCfg.buff[job] or {}
	for i, buffId in ipairs( jobBufs ) do
		--print("WorshipMonarchAddBuf, buffId="..buffId)
		if not Actor.hasBuffById( sysarg, buffId ) then
			Actor.addBuffById( sysarg, buffId )
		end
	end
end

function WorshipMonarchSendData( sysarg )
	local actorId  = Actor.getActorId( sysarg )
	local curFloor = WorshipMonarchGetCurFloor( actorId )
	local pack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sWorshipMonarchSendData)
    if pack then
        DataPack.writeByte(pack, curFloor)
        DataPack.flush(pack)
        --print("WorshipMonarchSendData, curFloor="..curFloor)
    end
end

InitTwelvePalacesRank()
